package com.example.broadcast

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.broadcast.databinding.ActivityLoginBinding
import com.example.broadcast.databinding.ActivityMainBinding
import com.example.broadcast.utils.BaseActivity

class LoginActivity : BaseActivity() {
    private lateinit var binding: ActivityLoginBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)

        var isRem = getSharedPreferences("data", Context.MODE_PRIVATE).getBoolean("isRem", false)
        if (isRem) {
            binding.accountEdit.setText(
                getSharedPreferences(
                    "data",
                    Context.MODE_PRIVATE
                ).getString("account", "")
            )
            binding.passwordEdit.setText(
                getSharedPreferences(
                    "data",
                    Context.MODE_PRIVATE
                ).getString("password", "")
            )
            binding.rememberPass.isChecked = true

        }

        binding.login.setOnClickListener {
            var account = binding.accountEdit.text.toString()
            var password = binding.passwordEdit.text.toString()
            if (account == "a" && password == "a") {
                if (binding.rememberPass.isChecked) {
                    getSharedPreferences("data", Context.MODE_PRIVATE).edit()
                        .putBoolean("isRem", true)
                        .putString("account", "a").putString("password", "a").apply()
                }

                var intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                finish()
            } else {
                Toast.makeText(this, "account or password is error", Toast.LENGTH_LONG).show()
            }

        }


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }
}